package com.example.administrator.yuejian35.Find.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.RecyclerView;

import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.Toast;

import com.example.administrator.yuejian35.Find.FindModel;
import com.example.administrator.yuejian35.Find.activity.AddPostActivity;
import com.example.administrator.yuejian35.Find.adapter.FindAdapter;
import com.example.administrator.yuejian35.Find.bean.Post;
import com.example.administrator.yuejian35.R;
import com.example.administrator.yuejian35.base.BaseFragment;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * A simple {@link Fragment} subclass.
 */
public class FindFragment extends BaseFragment {

    private FindAdapter adapter;
    private RecyclerView rv;

    private ImageButton addpost;

    public FindFragment() {
        // Required empty public constructor
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        rv = (RecyclerView) view.findViewById(R.id.rv_post);
        addpost = (ImageButton) view.findViewById(R.id.img_btn_addpost);

        addpost.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), AddPostActivity.class);
                startActivity(intent);
            }
        });

        FindModel findModel = new FindModel();
        Call call = findModel.postlist();

        call.enqueue(new Callback<List<Post>>() {

            @Override
            public void onResponse(Call<List<Post>> call, Response<List<Post>> response) {
                if(getActivity()!=null){

                    List<Post> post = response.body();
                    adapter = new FindAdapter(getActivity(), post);
                    int spanCount = 2;
                    StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(spanCount,
                            StaggeredGridLayoutManager.VERTICAL);
                    rv.setLayoutManager(layoutManager);
                    rv.setAdapter(adapter);
                    rv.setItemAnimator(new DefaultItemAnimator());
                }
            }

            @Override
            public void onFailure(Call<List<Post>> call, Throwable t) {
                Toast.makeText(getActivity(),"查询失败！！！",Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_find, container, false);
    }

}
